home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel PPC / lib_std / fixed_array.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  3.9 KB  |  212 lines  |  [TEXT/EDIT]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class FIXED_ARRAY[E]
  5.    -- 
  6.    -- Unlike ARRAY, the `lower' bound of a FIXED_ARRAY is
  7.    -- frozen (to 0). Thus, when looping toward 0, code may 
  8.    -- run (just) a little bit faster than ARRAY.
  9.    --
  10.    -- Note: a FIXED_ARRAY as only two attributes it save 
  11.    --       a little bit memory too :-)
  12.    --
  13.  
  14. inherit
  15.    COLLECTION[E]
  16.       redefine is_equal, fast_nb_occurrences, all_cleared
  17.       end;
  18.  
  19. creation {ANY}
  20.    make, resize, from_collection
  21.  
  22. feature
  23.  
  24.    lower: INTEGER is 0;
  25.      -- Lower index bound.
  26.    
  27.    upper: INTEGER;
  28.      -- Upper index bound.
  29.    
  30. feature {NONE}
  31.    
  32.    storage: POINTER;
  33.      -- Internal access to storage location.
  34.      -- Corresoponding C type is computed according 
  35.      -- to generic type E.
  36.    
  37. feature -- Creation and Modification :
  38.    
  39.    make(size: INTEGER) is
  40.       require
  41.      size >= 0
  42.       do
  43.      if storage.is_not_void then
  44.         storage := realloc(storage,size);
  45.      else
  46.         storage := malloc(size);
  47.      end;
  48.      upper := size - 1;
  49.      clear_all;
  50.       ensure
  51.      count = size;
  52.      all_cleared
  53.       end;
  54.  
  55.    from_collection(cltn: COLLECTION[E]) is
  56.       local
  57.      fa_index, cltn_index: INTEGER;
  58.       do
  59.      from
  60.         make(cltn.count);
  61.         fa_index := upper;
  62.         cltn_index := cltn.upper;
  63.      until
  64.         fa_index < 0
  65.      loop
  66.         put(cltn.item(cltn_index),fa_index);
  67.         cltn_index := cltn_index - 1;
  68.         fa_index := fa_index - 1;
  69.      end;
  70.       ensure
  71.      count = cltn.count
  72.       end;
  73.    
  74. feature -- Accessing :
  75.  
  76.    infix "@", item(index: INTEGER): E is
  77.       external "CSE"
  78.       end;
  79.    
  80. feature -- Modification :
  81.    
  82.    put(element: E; index: INTEGER) is
  83.       external "CSE"
  84.       end;
  85.  
  86.    clear is
  87.      -- Empty the array, discard all items.
  88.       do
  89.      upper := -1;
  90.       end;
  91.  
  92.    copy(other: like Current) is
  93.      -- Copy `other' onto Current.
  94.       local
  95.      i: INTEGER;
  96.       do
  97.      if upper /= other.upper then
  98.         make(other.upper);
  99.      end;
  100.      from
  101.         i := upper;
  102.      until
  103.         i = lower
  104.      loop
  105.         put(other.item(i),i);
  106.         i := i - 1;
  107.      end;
  108.       end;
  109.    
  110. feature -- Looking and comparison :
  111.  
  112.    is_equal(other: like Current): BOOLEAN is
  113.      -- Use `equal' to compare elements. 
  114.       local
  115.      i: INTEGER;
  116.      e1, e2: E;
  117.       do
  118.      if Current = other then
  119.         Result := true;
  120.      elseif upper = other.upper then
  121.         from
  122.            Result := true;
  123.            i := upper;
  124.         until
  125.            i < 0 or not Result
  126.         loop
  127.            Result := equal_like(item(i),other.item(i));
  128.            i := i - 1;
  129.         end;        
  130.      end;
  131.       end;
  132.  
  133.    all_cleared: BOOLEAN is
  134.      -- Are all items set to default values?
  135.       local
  136.      value: E;
  137.      i: INTEGER;
  138.       do
  139.      from  
  140.         Result := true;
  141.         i := upper;
  142.      until
  143.         i < lower
  144.      loop
  145.         Result := value = item(i);
  146.         if Result then
  147.            i := i - 1;
  148.         else
  149.            i := -1;
  150.         end;
  151.      end;
  152.       end;
  153.  
  154.    fast_nb_occurrences(elt: E): INTEGER is
  155.      -- Number of occurrences using `='.
  156.       local
  157.      i: INTEGER;
  158.       do
  159.      from  
  160.         i := upper;
  161.      until
  162.         i < 0
  163.      loop
  164.         if elt = item(i) then
  165.            Result := Result + 1;
  166.         end;
  167.         i := i - 1;
  168.      end;
  169.       end;
  170.  
  171. feature -- Interfacing with C :
  172.    
  173.    to_external: POINTER is
  174.      -- Gives C access into the internal `storage' of the ARRAY.
  175.      -- Result is pointing the element at index `lower'.
  176.      -- 
  177.      -- NOTE: do not free/realloc the Result. Resizing of the array 
  178.      --       can makes this pointer invalid. 
  179.       require
  180.      not empty
  181.       do
  182.      Result := storage;
  183.       ensure
  184.      Result.is_not_void
  185.       end;
  186.  
  187. feature {NONE}
  188.  
  189.    malloc(size: INTEGER): POINTER is
  190.       require
  191.      size > 0
  192.       local
  193.      x: like item;
  194.       do
  195.      c_inline_c("R=malloc((size_t)(a1*sizeof(_x)));");
  196.       end;
  197.  
  198.    realloc(pointer: POINTER; size: INTEGER): POINTER is
  199.       require
  200.      size > 0
  201.       local
  202.      x: like item;
  203.       do
  204.      c_inline_c("R=realloc(a1,(size_t)(a2*sizeof(_x)));");
  205.       end;
  206.  
  207. invariant
  208.    
  209.    upper >= lower implies storage /= Void;
  210.  
  211. end -- FIXED_ARRAY[E]
  212.